﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BusinessLogic.ProfileManagementLogic;
using Entities;

namespace MockProjectGroupThree.Presentation.ProfileManagement
{
    public partial class ClientManager : System.Web.UI.Page
    {
        ClientProfileManagementLogic clientProfile = new ClientProfileManagementLogic();
        private Client client;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadPage();
            }
            foreach (RepeaterItem item in RepeaterClient.Items)
            {
                var checkBoxDel = (CheckBox)item.FindControl("checkDelete");
                
                if(checkBoxDel.Checked)
                {
                    var labelClientID = (Label)item.FindControl("labelClientId");
                    LabelTestItem.Text = labelClientID.Text;
                }
            }
        }

        public int PageNumber
        {
            get
            {
                if (ViewState["PageNumber"] != null)
                    return Convert.ToInt32(ViewState["PageNumber"]);
                return 0;
            }
            set
            {
                ViewState["PageNumber"] = value;
            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            repeaterItems.ItemCommand += repeaterItems_ItemCommand;
        }

        private void LoadPage()
        {
            var dataTable = clientProfile.GetAllClient();
            //Starting paging
            var pagedDataSource = new PagedDataSource();
            var dataView = new DataView(dataTable);

            pagedDataSource.DataSource = dataView;
            pagedDataSource.AllowPaging = true;
            //show number of page
            pagedDataSource.PageSize = 6;
            pagedDataSource.CurrentPageIndex = PageNumber;
            //specify sum of page
            var numPage = pagedDataSource.PageCount;
            int currentPage;
            if (Request.QueryString["page"] != null)
            {
                currentPage = Int32.Parse(Request.QueryString["page"]);
            }
            else
            {
                currentPage = 1;
            }
            //because paging always start at 0
            pagedDataSource.CurrentPageIndex = currentPage - 1;
            //show
            Label1.Text = "Page " + currentPage + " of " + pagedDataSource.PageCount;
            //config
            if (!pagedDataSource.IsFirstPage)
                linkPrevious.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" +
                                           Convert.ToString(currentPage - 1);
            else
            {
                linkPrevious.Visible = false;
            }
            if (!pagedDataSource.IsLastPage)
            {
                linkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currentPage + 1);
            }
            else
            {
                linkNext.Visible = false;
            }
            // link array page
            if (pagedDataSource.PageCount > 1)
            {
                repeaterItems.Visible = true;
                var pages = new ArrayList();
                for (var i = 0; i < numPage; i++)
                {
                    pages.Add((i + 1).ToString(CultureInfo.InvariantCulture));
                }
                repeaterItems.DataSource = pages;
                repeaterItems.DataBind();
            }
            else
            {
                repeaterItems.Visible = false;
            }
            RepeaterClient.DataSource = pagedDataSource;
            RepeaterClient.DataBind();
        }

        void repeaterItems_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
            LoadPage();
        }

        protected void linkDelSelected_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in repeaterItems.Items)
            {
                var checkBoxDel = (CheckBox)item.FindControl("checkDelete");
                var labelClientID = (Label)item.FindControl("labelClientId");
                if (checkBoxDel != null)
                {
                    if (checkBoxDel.Checked)
                    {
                        client = new Client { ClientId = int.Parse(labelClientID.Text) };
                        var result = clientProfile.DeleteClient(client);
                        if (result > 0)
                        {
                            LoadPage();
                        }
                    }
                }
            }
        }

               
    }
}
